home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 016a / 4dtnt.zip / TODEC.BTM < prev    next >
Text File  |  1991-11-03  |  3KB  |  103 lines

  1.    Convert an operand (VALUE of an environment variable whose NAME is
  2.    given) to decimal fom the specified number base.  See BUMP for 
  3.    more details
  4.  
  5. if "%1" == "" .or. "%2" == "" .or. "%[%2]" == "" goto help
  6. set $foo=%@upper[%1]
  7. set $wstr=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
  8.  
  9.    If the base is given by mnemonic letter, convert it to the
  10.    appropriate number.
  11.  
  12. iff      %$foo = B then ^ set $foo=2
  13.  elseiff %$foo = O then ^ set $foo=8
  14.  elseiff %$foo = H then ^ set $foo=16
  15.  elseiff %$foo = D then ^ set $foo=10
  16. endiff
  17.  
  18.    Validate the base of the number system and call the conversion if OK.
  19.  
  20. iff %$foo gt 1 .and. %$foo lt 37 then ^ gosub base
  21.  else goto help
  22. endiff
  23.  
  24.    Check the result of conversion and return it if valid.
  25.  
  26. iff %$val ne 0 then^unset %2 ^ (gosub cleanup^goto helpval)
  27.  else set %2=%$foo ^gosub cleanup^quit 0
  28. endiff
  29. quit
  30.  
  31. :cleanup
  32. unset $foo $wstr $val $max >& nul
  33. return
  34.  
  35. :base
  36. set $max=%$foo
  37. gosub cnvtit
  38. return
  39.  
  40.    The heart of the batch -- the actual validation and conversion.
  41.  
  42. :cnvtit
  43.  
  44.    Set all input characters to upper case to standardize
  45. set $foo=%@upper[%[%2]]
  46.  
  47.    Limit the length of the loop to the length of the string
  48. set $i=%@eval[%@len[%$foo]-1]
  49.    
  50.    Set conversion multiplier to 1 for rightmost digit
  51. set $mult=1
  52.  
  53.    Initialize value accumulator
  54. set $accum=0
  55.  
  56. :cnvtloop
  57.  
  58.    The base value of the current digit is its offset into wstr.  If
  59.    that value is greater than the number system base or -1 (no such
  60.    character) drop dead because of the invalid character.
  61.  
  62. set $val=%@index[%$wstr,%@substr[%[%2],%$i,1]]
  63. if %$val lt 0 .or. %$val ge %$max (set $val=%$max^goto cnvtexit)
  64.  
  65.    Multiply the base value by the multiplier and add to the accumulator.
  66. set $accum=%@eval[%$accum+%@eval[%$val*%$mult]]
  67.  
  68.    Change the multiplier to the value of digits in the next left column.
  69. set $mult=%@eval[%$mult*%$max]
  70.  
  71.    Point at the next left character
  72. set $i=%@eval[%$i-1]
  73.  
  74.    See if we're done.
  75. if %$i ge 0 goto cnvtloop
  76.  
  77.    Return the accumulated total.
  78. set $foo=%$accum
  79.  
  80.    Set OK result indication.
  81. set $val=0
  82. :cnvtexit
  83. unset $i $mult $accum >& nul
  84. return
  85.  
  86. :helpval
  87. echo Invalid character detected.
  88. echo .
  89. goto help
  90.  
  91. :help
  92. echo Usage: %@name[%0]  mode  var
  93. echo.
  94. echo   var =  Any environment variable whose value is the input
  95. echo   mode = any of:
  96. echo       B=binary    [0-1]
  97. echo       D=decimal   [0-9]
  98. echo       H=hex       [0-F]
  99. echo       O=octal     [0-7]
  100. echo       [2-36]      [0-(n-1)]
  101. echo New value returned in var, replacing original value
  102. quit 4
  103.